package com.vgemv.jsutilitysdk.Request.JsRequestManager2;

import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestBaseData.JsRequestBodyInputType;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestBaseData.JsResponseBodyOutputType;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestBaseData.JsRequestMethod;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class JsRequestConfig {

    public JsRequestMethod requestMethod;

    //传入的请求体的数据类型
    public JsRequestBodyInputType requestBodyInputType;
    //传入的请求体数据
    public Object requestBodyInput;

    //server的地址
    public String serverAddress;

    //接口相对服务器的路径
    public String interfacePath;

    public HashMap urlParam;

    //ServerAddress/InterfacePah
    private String interfaceUrlString;

    //interfaceUrlString?a=1&b=1
    private String requestUrlString;

    public JsResponseBodyOutputType responseBodyOutputType;

    public Integer timeOut;

    public JsRequestConfig(JsRequestMethod requestMethod,JsRequestBodyInputType requestBodyInputType,JsResponseBodyOutputType responseBodyOutputType) {
        this.requestMethod = requestMethod;
        this.requestBodyInputType = requestBodyInputType;
        this.responseBodyOutputType = responseBodyOutputType;
    }

    public String getInterfaceUrlString() {
        String result = interfaceUrlString;
        if(result == null) {
            result = String.format("%s/%s",serverAddress,interfacePath);
        }
        return result;
    }

    public String getRequestUrlString() {
        String result = requestUrlString;
        if(result == null) {
            StringBuilder sb_urlParamString = new StringBuilder();
            if(urlParam != null) {
                Set<Map.Entry<String,String>> entrySet = urlParam.entrySet();
                int index = 0;
                for (Map.Entry<String,String>entry : entrySet) {
                    if(index != 0) {
                        sb_urlParamString.append("&");
                    }
                    sb_urlParamString.append(String.format("%s=%s",entry.getKey(),entry.getValue()));
                }
                String urlParamString = sb_urlParamString.toString();
                result = String.format("%s?%s",getInterfaceUrlString(),urlParamString);
            }
            else {

            }
            result = getInterfaceUrlString();
        }
        return result;
    }

    public void setRequestUrlString(String requestUrlString) {
        this.requestUrlString = requestUrlString;
    }

    public void setInterfaceUrlString(String interfaceUrlString) {
        this.interfaceUrlString = interfaceUrlString;
    }
}
